#ifdef WIN32

#include "Network/UDPClient.h"

#include "Platform/Socket/WindowsSocketCommon.h"

#include <WinSock2.h>

CUDPClient::CUDPClient()
{
    PlatformSocket = (void* )socket(
        AF_INET
        , SOCK_DGRAM
        , IPPROTO_UDP);
    assert((SOCKET)PlatformSocket != INVALID_SOCKET && "socket failed");

    //set non-blocking
    u_long NonBlocking = 1;
    int32_t Result = ioctlsocket((SOCKET)PlatformSocket, FIONBIO, &NonBlocking);

}

CUDPClient::~CUDPClient()
{
    closesocket((SOCKET)PlatformSocket);
}

void CUDPClient::Send(const TVector<uint8_t>& Data,const SIPAddress & IP, uint16_t Port)
{
    sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = IP.IsIPv6()?AF_INET6:AF_INET;
    addr.sin_port = htons(Port);
    if (IP.IsIPv6())
    {
        memcpy(&addr.sin_addr, IP.IP.IPv6, sizeof(IP.IP.IPv6));
    }
    else
    {
        memcpy(&addr.sin_addr, IP.IP.IPv4, sizeof(IP.IP.IPv4));
    }

    int32_t Result = sendto((SOCKET)PlatformSocket, (const char*)Data.Data(), Data.Num(), 0, (sockaddr*)&addr, sizeof(addr));
    assert(Result == Data.Num() && "sendto failed");
}





#endif